package com.wangijun.yuan.gen;


import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.nio.file.Paths;

/*代码生成*/

public class Gen {
    public static void main(String[] args) {
        String pack = "com.wangijun.yuan.admin.blog";
        FastAutoGenerator.create("jdbc:mysql://xxxxx:3306/yuan-blog?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false&serverTimezone=Asia/Shanghai",
                        "root",
                        "xxxxx")
                .globalConfig(builder -> builder
                        .author("Wlf")
                        .disableOpenDir()
                        .enableSpringdoc()
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/admin/src/main/java")
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent(pack)
                        .entity("entity")
                        .mapper("mapper")
                        .xml("mapper.xml")

                )
                .strategyConfig(builder -> builder
                        .addExclude("file_detail")
                        .entityBuilder()
                        .enableLombok()
                        .enableTableFieldAnnotation()
                        .idType(IdType.ASSIGN_ID)
                        .addTableFills(
                                new Column("createTime", FieldFill.INSERT),
                                new Column("updateTime", FieldFill.INSERT_UPDATE),
                                new Column("createBy", FieldFill.INSERT),
                                new Column("updateBy", FieldFill.INSERT_UPDATE)
                        )
                        .serviceBuilder()
                        .disableService()
                        .disableServiceImpl()
                        .controllerBuilder()
                        .enableRestStyle()
                        .template("/templates/controller.java")

                )
                .injectionConfig(consumer -> {
                    consumer.customFile(cf -> cf
                            .fileName(".java")
                            .formatNameFunction(tableInfo -> tableInfo.getEntityName() + "QryParam")
                            .packageName("model.dto")
                            .templatePath("/templates/entityQryParam.java.ftl")
                            .build()
                    );
                    consumer.customMap(Dict.create()
                            .set("wParentName", pack));
                    consumer.customFile(cf -> cf
                            .fileName(".java")
                            .formatNameFunction(tableInfo -> tableInfo.getEntityName() + "SaveParam")
                            .packageName("model.dto")
                            .templatePath("/templates/entitySaveParam.java.ftl")
                            .build()
                    );

                    consumer.customFile(cf -> cf
                            .fileName(".java")
                            .formatNameFunction(tableInfo -> tableInfo.getEntityName() + "UpdateParam")
                            .packageName("model.dto")
                            .templatePath("/templates/entityUpdateParam.java.ftl")
                            .build()
                    );
                    consumer.customFile(cf -> cf
                            .fileName(".java")
                            .formatNameFunction(tableInfo -> tableInfo.getEntityName() + "VO")
                            .packageName("model.vo")
                            .templatePath("/templates/entityVO.java.ftl")
                            .build()
                    );
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
